package dbs;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 数据库公共类
 * 
 * @author Beanstao@gmail.com
 * @date 2013-10-15下午3:44:02
 * @version 1.0
 */
public class DBCommon
{
	/**
	 * Display the driver properties, database details
	 */
	// =========================================================MySQL================
	// Driver Information
	// Driver Name: MySQL-AB JDBC Driver
	// Driver Version: mysql-connector-java-5.1.13 ( Revision: ${bzr.revision-id} )
	//
	// Database Information
	// Database Name: MySQL
	// Database Version: 5.6.10-log
	//
	// Avalilable Catalogs
	// catalog: information_schema
	// catalog: com_pan_soms520_cache
	// catalog: com_pan_soms520_customer
	// catalog: com_pan_soms520_device
	// catalog: com_pan_soms520_log
	// catalog: com_pan_soms520_memcache
	// catalog: com_pan_soms520_operation
	// catalog: com_pan_soms520_permission
	// catalog: mysql
	// catalog: performance_schema
	// catalog: test
	// =========================================================MySQL================

	// =========================================================SQLServer============
	// Driver Information
	// Driver Name: Microsoft JDBC Driver 4.0 for SQL Server
	// Driver Version: 4.0.2206.100
	//
	// Database Information
	// Database Name: Microsoft SQL Server
	// Database Version: 10.50.4000
	//
	// Avalilable Catalogs
	// catalog: master
	// catalog: model
	// catalog: msdb
	// catalog: tempdb
	// =========================================================SQLServer============
	public static void displayDbProperties(Connection connection)
	{
		java.sql.DatabaseMetaData dm = null;
		java.sql.ResultSet resultSet = null;
		try
		{
			if (connection != null)
			{
				dm = connection.getMetaData();
				System.out.println("Driver Information");
				System.out.println("\tDriver Name: " + dm.getDriverName());
				System.out.println("\tDriver Version: " + dm.getDriverVersion());

				System.out.println("\nDatabase Information ");
				System.out.println("\tDatabase Name: " + dm.getDatabaseProductName());
				System.out.println("\tDatabase Version: " + dm.getDatabaseProductVersion());

				System.out.println("Avalilable Catalogs ");
				resultSet = dm.getCatalogs();
				while (resultSet.next())
				{
					System.out.println("\tcatalog: " + resultSet.getString(1));
				}
			}
			else
			{
				System.out.println("Error: No active Connection");
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			closeResultSet(resultSet);
			dm = null;
		}
	}

	public static void close(Connection connection, ResultSet resultSet)
	{
		close(connection, null, resultSet);
	}

	public static void close(Connection connection, Statement statement)
	{
		close(connection, statement, null);
	}

	public static void close(Connection connection, Statement statement, ResultSet resultSet)
	{
		if (connection != null)
		{
			closeConnection(connection);
		}
		if (statement != null)
		{
			closeStatement(statement);
		}
		if (resultSet != null)
		{
			closeResultSet(resultSet);
		}
	}

	public static void closeConnection(Connection connection)
	{
		if (connection != null)
		{
			try
			{
				connection.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void closeStatement(Statement statement)
	{
		if (statement != null)
		{
			try
			{
				statement.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void closeResultSet(ResultSet resultSet)
	{
		if (resultSet != null)
		{
			try
			{
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}
